/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.baseConfig.controller;

import com.koron.workflow.baseConfig.bean.RoleLineBean;
import com.koron.workflow.baseConfig.bean.RoleLineTeamBean;
import com.koron.workflow.baseConfig.bean.RoleLineTeamUserBean;
import com.koron.workflow.baseConfig.service.RoleLineService;
import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @author qingfeng
 * @description: TODO
 * @date 2020/4/13 14:15
 */
@Api(value = "roleLine", description = "角色")
@RestController
@RequestMapping("/roleLine")
public class RoleLineController {

    @Autowired
    private RoleLineService service;

    private static final Logger logger = LoggerFactory.getLogger(RoleLineController.class);

    @ApiOperation("角色分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("/queryForList.htm")
    public MessageBean queryForList(@ApiIgnore String name, @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null) {
            page = page - 1;
            page = page * pageSize;
            if (page < 0) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean<RoleLineBean> pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    name, page, pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询角色失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("保存角色")
    @PostMapping("/save.htm")
    public MessageBean save(@ApiIgnore @CaseUser StaffBean user, @RequestBody @Valid RoleLineBean bean) {
        try {
            return ADOConnection.runTask(service, "save", MessageBean.class, bean, user);
        } catch (Exception e) {
            logger.error("保存角色失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("根据ID删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "岗位id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String id) {
        try {
            ADOConnection.runTask(service, "delete", void.class, id);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        } catch (Exception e) {
            logger.error("删除角色失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/queryForObject.htm")
    public MessageBean queryForObject(@ApiIgnore String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        try {
            RoleLineBean data = ADOConnection.runTask(service, "queryForObject", RoleLineBean.class, id);
            msg.setData(data);
            return msg;
        } catch (Exception e) {
            logger.error("查询失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("保存团队")
    @PostMapping("/insertRoleLineTeam.htm")
    public MessageBean insertRoleLineTeam(@RequestBody @Valid RoleLineTeamBean bean) {
        try {
            ADOConnection.runTask(service, "insertRoleLineTeam", MessageBean.class, bean);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("根据团队id删除团队")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleLineTeamId", value = "团队id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/deleteRoleLineTeam.htm")
    public MessageBean deleteRoleLineTeam(@ApiIgnore String roleLineTeamId) {
        try {
            ADOConnection.runTask(service, "deleteRoleLineTeam", void.class, roleLineTeamId);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        } catch (Exception e) {
            logger.error("删除失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("删除团队用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/deleteRoleLineTeamUser.htm")
    public MessageBean deleteRoleLineTeamUser(@ApiIgnore String id) {
        try {
            ADOConnection.runTask(service, "deleteRoleLineTeamUser", void.class, id);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        } catch (Exception e) {
            logger.error("删除失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("批量保存团队用户")
    @PostMapping("/batchInsertRoleLineTeamUser.htm")
    public MessageBean batchInsertRoleLineTeamUser(@RequestBody List<RoleLineTeamUserBean> list, String teamId) {
        try {
            ADOConnection.runTask(service, "batchInsertRoleLineTeamUser", void.class, list, teamId);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), String.class);
        }
    }

    @ApiOperation("预览")
    @GetMapping("/review.htm")
    public MessageBean review(@ApiIgnore String roleId) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try {
            List<RoleLineTeamBean> data = ADOConnection.runTask(service, "review", List.class, roleId);
            msg.setData(data);
        } catch (Exception e) {
            logger.error("预览", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), List.class);
        }
        return msg;
    }

    @ApiOperation("根据角色id查询团队列表")
    @GetMapping("/queryTeamByRoleId.htm")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String", paramType = "query"),
    })
    public MessageBean queryTeamByRoleId(@ApiIgnore String roleId) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try {
            List<RoleLineTeamBean> data = ADOConnection.runTask(service, "queryTeamByRoleId", List.class, roleId);
            msg.setData(data);
        } catch (Exception e) {
            logger.error("查询失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), List.class);
        }
        return msg;
    }

    @ApiOperation("根据团队id用户列表")
    @GetMapping("/queryTeamUserByTeamId.htm")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamId", value = "团队id", required = true, dataType = "String", paramType = "query"),
    })
    public MessageBean queryTeamUserByTeamId(@ApiIgnore String teamId) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try {
            List<RoleLineTeamUserBean> data = ADOConnection.runTask(service, "queryTeamUserByTeamId", List.class, teamId);
            msg.setData(data);
        } catch (Exception e) {
            logger.error("查询失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), List.class);
        }
        return msg;
    }
}
